<?php

class Cdn_user extends CI_Model {

	function __construct() {
		parent::__construct();
	}

	function add($cond) {
		if ($this->exist($cond['username'])) return FALSE;
		$cond['password'] = md5($cond['password']);
		$cond['status'] = 1;
		$cond['created_at'] = date('Y-m-d h:i:s', mktime());
		$cond['updated_at'] = date('Y-m-d h:i:s', mktime());
		$this->db->insert('cdn_user', $cond);
		return $this->db->insert_id();
	}

	function del($id) {
		$data = array('status' => 0);
		$data['updated_at'] = date('Y-m-d h:i:s', mktime());
		$this->db->where('id', $id);
		$this->db->update('cdn_user', $data); 
	}

	function mdel($id) {
		$data = array('status' => 0);
		$data['updated_at'] = date('Y-m-d h:i:s', mktime());
		$this->db->where_in('id', $id);
		$this->db->update('cdn_user', $data); 
	}

	function up($id, $data) {
		unset($data['id']);
		if (array_key_exists('password', $data)) $data['password'] = md5($data['password']);
		$data['updated_at'] = date('Y-m-d h:i:s', mktime());
		$this->db->where_in('id', $id);
		$this->db->update('cdn_user', $data); 
	}

	function find($id) {
		$this->db->where('id', $id); 
		$query = $this->db->get('cdn_user');
		return $query->result_array();
	}

	function nfind($username) {
		$this->db->where('username', $username); 
		$query = $this->db->get('cdn_user');
		return $query->result_array();
	}

	function mfind($id) {
		$this->db->where_in('id', $id); 
		$query = $this->db->get('cdn_user');
		return $query->result_array();
	}

	function cfind($cond, $start = 10, $per_page = 10) {
		$this->db->where($cond); 
		$this->db->limit($start, $per_page);
		$this->db->order_by('id', 'desc');
		$query = $this->db->get('cdn_user');
		return $query->result_array();
	}

	function auth($username, $password) {
		$cond = array('username' => $username, 'password' => md5($password), 'status' => 1, 'atype' => 1);
		$this->db->where($cond);
		$query = $this->db->get('cdn_user');
		if ($query->num_rows() >= 1) return TRUE;
		return FALSE;	
	}

	function exist($username) {
		$cond = array('username' => $username);
		$this->db->where($cond);
		$query = $this->db->get('cdn_user');
		if ($query->num_rows() >= 1) return TRUE;
		return FALSE;	
	}

	function filter($data, $act = 'create') {
		$msg = array();

		if (!is_array($data) OR empty($data)) $msg[] = "输入数据错误";

		if (!array_key_exists('username', $data) OR $data['username'] == '') $msg[] = "用户名不能为空";
		if (!array_key_exists('password', $data) OR $data['password'] == '') $msg[] = "密码不能为空";
		if (!array_key_exists('atype', $data) OR $data['atype'] == '') $msg[] = "密码不能为空";

		foreach ($data as $k => $v) {
			if (!$v) {
				unset($data[$k]);
				continue;
			}
			switch($k) {
				case 'username':
					if ($this->exist($v)) $msg[] = "用户名已经存在";
					if (strlen($v) > 32) $msg[] = "用户名超过32个字节";
					break;
				case 'password':
					if (strlen($v) > 32) $msg[] = "密码超过32个字节";
					break;
				case 'atype':
					if (!in_array(intval($v), array(1, 2))) $msg[] = "用户类型非法";
					break;
				case 'email':
					$this->load->helper('email');
					if (!valid_email($v)) $msg[] = "Email格式非法";
					break;
				case 'mobile':
					if (!is_numeric($v)) $msg[] = "手机号码格式非法";
					break;
			}
		}
		$status = FALSE;
		if (empty($msg)) {
			$status = TRUE;
		}
		return array('status' => $status, 'msg' => $msg);
	}

	function efilter($data, $act = 'create') {
		$msg = array();

		if (!is_array($data) OR empty($data)) $msg[] = "输入数据错误";

		//if (!array_key_exists('username', $data) OR $data['username'] == '') $msg[] = "用户名不能为空";
		//if (!array_key_exists('password', $data) OR $data['password'] == '') $msg[] = "密码不能为空";
		if (!array_key_exists('atype', $data) OR $data['atype'] == '') $msg[] = "密码不能为空";

		foreach ($data as $k => $v) {
			if (!$v) {
				unset($data[$k]);
				continue;
			}
			switch($k) {
				case 'username':
					if (!$this->exist($v)) $msg[] = "用户名不存在";
					if (strlen($v) > 32) $msg[] = "用户名超过32个字节";
					break;
				case 'password':
					if (strlen($v) > 32) $msg[] = "密码超过32个字节";
					break;
				case 'atype':
					if (!in_array(intval($v), array(1, 2))) $msg[] = "用户类型非法";
					break;
				case 'email':
					$this->load->helper('email');
					if (!valid_email($v)) $msg[] = "Email格式非法";
					break;
				case 'mobile':
					if (!is_numeric($v)) $msg[] = "手机号码格式非法";
					break;
			}
		}
		$status = FALSE;
		if (empty($msg)) {
			$status = TRUE;
		}
		return array('status' => $status, 'msg' => $msg);
	}

	function utotal() {
		$cond = array('status' => 1, 'atype' => 1);
		$this->db->where($cond);
		$query = $this->db->get('cdn_user');
		return $query->num_rows();
	}

	function gtotal() {
		$cond = array('status' => 1, 'atype' => 2);
		$this->db->where($cond);
		$query = $this->db->get('cdn_user');
		return $query->num_rows();
	}

	function all_user($start = 0, $per_page = 10) {
		$cond = array('atype' => 1, 'status' => 1);	
		$this->db->where($cond); 
		$this->db->order_by('id', 'desc');
		$this->db->limit($per_page, $start);
		$query = $this->db->get('cdn_user');
		return $query->result_array();
	}

	function all_group($start = 0, $per_page = 10) {
		$cond = array('atype' => 2, 'status' => 1);	
		$this->db->where($cond); 
		$this->db->order_by('id', 'desc');
		$this->db->limit($per_page, $start);
		$query = $this->db->get('cdn_user');
		return $query->result_array();
	}

	function user_group_exist($user_id, $group_id) {
		$cond = array('user_id' => $user_id, 'group_id' => $group_id, 'status' => 1);
		$this->db->where($cond);
		$query = $this->db->get('user_group');
		if ($query->num_rows() >= 1) return TRUE;
		return FALSE;	
	}

	function user_group_filter($data) {
		$msg = array();
		if (!array_key_exists('user_id', $data)) $msg[] = '用户ID为空';
		if (!array_key_exists('group_id', $data)) $msg[] = '用户组ID为空';
		if (!array_key_exists('role', $data)) $msg[] = '用户组内角色为空';
		if (!$this->find(intval($data['user_id']))) $msg[] = '用户不存在';
		if (!$this->find(intval($data['group_id']))) $msg[] = '用户不存在';
		if (!in_array($data['role'], array(0,1))) $msg[] = '组内角色非法';
		if ($this->user_group_exist($data['user_id'], $data['group_id'])) $msg[] = '已经存在';
		$status = FALSE;
		if (empty($msg)) {
			$status = TRUE;
		}
		return array('status' => $status, 'msg' => $msg);
	}

	function join_group($data) {
		if ($this->user_group_exist($data['user_id'], $data['group_id'])) return FALSE;
		$cond = array(
			'user_id' => $data['user_id'],
			'group_id' => $data['group_id'],
			'role' => $data['role'],
			'status' => 1
		);
		$cond['created_at'] = date('Y-m-d h:i:s', mktime());
		$cond['updated_at'] = date('Y-m-d h:i:s', mktime());
		$this->db->insert('user_group', $cond);
		return $this->db->insert_id();
	}

	function out_group($cond) {
		if (!$this->user_group_exist($cond['user_id'], $cond['group_id'])) return TRUE;
		$data = array('status' => 0);
		$data['updated_at'] = date('Y-m-d h:i:s', mktime());
		$this->db->where(array('user_id' => $cond['user_id'], 'group_id' => $cond['group_id']));
		$this->db->update('user_group', $data); 
	}

	function member($group_id) {
		$this->db->where(array('group_id' => $group_id, 'status' => 1)); 
		$this->db->order_by('id', 'desc');
		$query = $this->db->get('user_group');
		$tmp = $query->result_array();
		$user_id = array();
		if (empty($tmp)) return $user_id;	
		foreach ($tmp as $k => $v) {
			$user_id[] = $v['user_id'];
		}
		return $this->mfind($user_id);
	}

	function leader($group_id) {
		$this->db->where(array('group_id' => $group_id, 'status' => 1, 'role' => 1)); 
		$this->db->order_by('created_at', 'desc');
		$query = $this->db->get('user_group');
		$tmp = $query->result_array();
		$user_id = array();
		if (empty($tmp)) return $user_id;	
		foreach ($tmp as $k => $v) {
			$user_id[] = $v['user_id'];
		}
		return $user_id;
	}
}

